// pages/forgetPass/forgetPass.js
var app = getApp();
var com = require('../../common.js')
Page({
  data: {
    phoneNum: '获取验证码',
    bindEndClick: 'clickEnd',
    number: 61,
    imageCode: '',
    imageImg: '',
    code: '',
    phoneKey: '',
    keynumber: '',
    showuum: true
  },
  onLoad: function(options) {
    var _this = this
    _this.onObtain()
  },
  onReady: function() {},
  onShow: function() {},
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  onShareAppMessage: function() {},
  //刷新验证码
  f5codeImg() {
    var _ = this
    _.onObtain()
  },
  //获取验证码
  onObtain() {
    var _this = this,
      url = app.link.obtain,
      data = {}
    com.link(url, data, function(rep) {
      console.log(rep)
      _this.setData({
        imageCode: rep.data.response.captcha.key,
        imageImg: rep.data.response.captcha.img,
      })
    }, 'GET')
  },
  //获取输入的手机号
  phoneNumber(e) {
    var _this = this,
      id = e.currentTarget.id,
      value = e.detail.value
    if (id == 1) {
      var l = value.split('')
      console.log(l)
      if (l.length < 11) {
        wx.showToast({
          title: '请输入正确手机号',
          icon: 'none'
        })
      } else {
        _this.setData({
          phoneNumber: value
        })
      }
    } else if (id == 2) {
      _this.setData({
        code: value
      })
    } else if (id == 3) {
      _this.setData({
        passwor: value
      })
    } else if (id == 4) {
      if (_this.data.passwor != e.detail.value) {
        console.log(_this.data.passwor, e.detail.value)
        wx.showToast({
          title: '输入密码不一致',
          icon: 'none'
        })
        _this.setData({
          showuum: false
        })
      } else {
        _this.setData({
          showuum: true
        })
      }

    } else {
      _this.setData({
        keynumber: value
      })
    }
  },
  //点击倒计时
  clickEnd() {
    wx.showToast({
      icon: 'loading',
      title: '发送中',
    })
    var _this = this,
      data = {
        captcha: _this.data.code,
        phone_number: _this.data.phoneNumber,
        key: _this.data.imageCode,
      },
      url = app.link.basics
    _this.setData({
      show: true
    })
    com.link(url, data, function(rep) {
      console.log(rep.data.response.captcha)
      if (rep.data.state == false) {
        com.toast(rep.data.message)
        _this.setData({
          show: false
        })
      } else {
        com.toast(rep.data.message)
        console.log(rep.data.response.key)
        _this.setData({
          phoneKey: rep.data.response.key,
          show: false
        })
        console.log(_this.data.phoneKey)
        wx.showToast({
          title: '发送成功',
        })
        _this.onSetTimeout()
      }
    })
  },
  //倒计时
  onSetTimeout() {
    console.log(1)
    var _this = this
    var numberNum = parseInt(_this.data.number)
    numberNum = numberNum - 1
    if (_this.data.number == 0) {
      _this.setData({
        phoneNum: "重新获取",
        bindEndClick: 'clickEnd',
        number: 61
      })
      return false
    } else {
      _this.setData({
        phoneNum: numberNum + '秒后重新获取',
        bindEndClick: '',
        number: numberNum
      })
    }
    setTimeout(() => {
      _this.onSetTimeout()
      console.log(1)
    }, 1000)
  },
  next() {
    var _this = this
    com.link(app.link.forget, {
      phone_number: _this.data.phoneNumber,
      captcha: _this.data.keynumber,
      key: _this.data.phoneKey,
      password: _this.data.k
    }, function(rep) {
      console.log(rep)
      if (rep.data.state) {
        wx.showModal({
          title: '温馨提示',
          content: rep.data.message,
          success(res) {
            if (res.confirm) {
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }
          }
        })
      } else {
        com.toast(rep.data.message)
      }
    })
  }
})